# ==== 基础配置 ====

RED 					:= $(shell tput setaf 1 2>/dev/null || echo "\033[0;31m")
RESET 					:= $(shell tput sgr0 2>/dev/null || echo "\033[0m")
SRC_DIR                 := src
OUT_DIR                 := build
LAB_NAME                := gz-utils-native
LAB_NAME_1              := gz-utils-native

TARGET_PLATFORM         ?= unknown
TARGET_ARCH             ?= unknown
SRC_FILES 				:= $(shell find $(SRC_DIR) -name '*.cpp')
SRC_INCLUDES 			:= $(sort $(dir $(shell find $("include") -name '*.h')))
#SRC_INCLUDES 			:= include/jni/com_xiyuan_smartutils_reflect_JavaUnsafe.h
define get-h
$(sort $(dir $(shell find $(1) -name '*.h')))
endef

# 自动递归查找所有 .c 和 .cpp 文件
#C_SRC       := $(shell find $(SRC_DIR) -name '*.c')
#CPP_SRC     := $(shell find $(SRC_DIR) -name '*.cpp')
#SRC         := $(C_SRC) $(CPP_SRC)

CC                      := gcc
CXX                     := g++
EXT                     := .so
SHARED_FLAG             := -shared
CC_ARGS                 :=
JAVA_HOME               ?= /opt/java/jdk8/jdk1.8.0_451
JNI_INCLUDE_DIR         ?= /opt/java/jdk8/include
NDK_HOME                ?= /opt/android-ndk-r25b
MAKE_DIR                := unknown
JNI_INCLUDE				:= -I$(JAVA_HOME)/include
define get-lib-name
$(LAB_NAME)_$(TARGET_PLATFORM)-$(TARGET_ARCH)
endef

TARGET_ARCH             := unknown
# 系统
# linux 系列
ifneq (,$(filter $(PLATFORM), linux))
    TARGET_PLATFORM         := linux
    LAB_NAME				:= lib$(LAB_NAME)

    EXT                     := .so
    ifneq (,$(filter $(ARCH), arm64 aarch64))
        TARGET_ARCH         := aarch64
        JNI_INCLUDE			:= $(JNI_INCLUDE) -I$(JNI_INCLUDE_DIR)/linux-aarch64/include/linux
        CC                  := aarch64-linux-gnu-gcc
        CXX                 := aarch64-linux-gnu-g++
    endif
    ifneq (,$(filter $(ARCH), armhf armv7 armv7l armv7hf))
        TARGET_ARCH         := armhf_v7_v7l_v7hf
        JNI_INCLUDE			:= $(JNI_INCLUDE) -I$(JNI_INCLUDE_DIR)/linux-aarch64/include/linux
        LAB_NAME			:= lib$(LAB_NAME_1)
        CC                  := arm-linux-gnueabihf-gcc
        CXX                 := arm-linux-gnueabihf-g++
    endif
    ifneq (,$(filter $(ARCH), arm))
            TARGET_ARCH         := arm
            JNI_INCLUDE			:= $(JNI_INCLUDE) -I$(JNI_INCLUDE_DIR)/linux-aarch64/include/linux
            LAB_NAME			:= lib$(LAB_NAME_1)
            CC                  := arm-linux-gnueabi-gcc
            CXX                 := arm-linux-gnueabi-g++
        endif
    ifneq (,$(filter $(ARCH), x64 x86_64))
        TARGET_ARCH         := x86_64
        JNI_INCLUDE			:= $(JNI_INCLUDE) -I$(JNI_INCLUDE_DIR)/linux-x64/include/linux
        CC                  := x86_64-linux-gnu-gcc
        CXX                 := x86_64-linux-gnu-g++
    endif
    ifneq (,$(filter $(ARCH), x86 x32))
        TARGET_ARCH         := x86
        JNI_INCLUDE			:= $(JNI_INCLUDE) -I$(JNI_INCLUDE_DIR)/linux-x86/include/linux
        CC                  := gcc -m32
        CXX                 := g++ -m32
    endif
endif
# windows
ifneq (,$(filter $(PLATFORM), win windows))
    TARGET_PLATFORM         := windows
    EXT                     := .dll

    JNI_INCLUDE				:= -I$(JNI_INCLUDE_DIR)/windows-x64/include
    JNI_INCLUDE				:= -I$(JNI_INCLUDE_DIR)/windows-x64/include/win32 $(JNI_INCLUDE)
    CC_ARGS             	:= -static \
                               -static-libgcc \
                               -static-libstdc++ \
                               -Wl,-Bstatic \
                               -lwinpthread \
                               -Wl,-Bdynamic \
                               -Os \
                               -s \
                               -DNDEBUG \
                               -lstdc++ \
                               -lgcc \

    ifneq (,$(filter $(ARCH), arm64 aarch64))
		LLVM_PATH           := /opt/llvm-mingw/bin
		TARGET_ARCH         := aarch64
		CC                  := $(LLVM_PATH)/aarch64-w64-mingw32-clang
		CXX                 := $(LLVM_PATH)/aarch64-w64-mingw32-clang++
		CC_ARGS             := -static \
                               -Wl,-Bstatic \
                               -lc++ -lunwind \
                               -Wl,-Bdynamic \
                               -lwinpthread \
                               -Os \
                               -s \
                               -nostdlib++ \

	endif
	ifneq (,$(filter $(ARCH), armhf armv7 armv7l armv7hf aarch32))
		LLVM_PATH           := /opt/llvm-mingw/bin
		TARGET_ARCH         := armhf_v7_v7l_v7hf
		JNI_INCLUDE			:= -I$(JNI_INCLUDE_DIR)/windows-x32/include/win32 $(JNI_INCLUDE)
		CC                  := $(LLVM_PATH)/armv7-w64-mingw32-gcc
		CXX                 := $(LLVM_PATH)/armv7-w64-mingw32-g++
		CC_ARGS				:=
	endif
    ifneq (,$(filter $(ARCH), x64 x86_64))
        TARGET_ARCH         := x86_64
        CC                  := x86_64-w64-mingw32-gcc
        CXX                 := x86_64-w64-mingw32-g++
    endif
    ifneq (,$(filter $(ARCH), x86 x32))
	 	TARGET_ARCH         := x86
	 	JNI_INCLUDE			:= -I$(JNI_INCLUDE_DIR)/windows-x32/include/win32 $(JNI_INCLUDE)
	    CC                  := i686-w64-mingw32-gcc
	    CXX                 := i686-w64-mingw32-g++
 endif
endif
#  macOS
ifneq (,$(filter $(PLATFORM), mac macOS macos macosx))
    LAB_NAME				:= lib$(LAB_NAME)
    TARGET_PLATFORM         := macos
    EXT                     := .dylib
    ifneq (,$(filter $(ARCH), x64 x86_64))
        TARGET_ARCH         := x86_64
        JNI_INCLUDE			:= -I$(JNI_INCLUDE_DIR)/macosx-x64/include -I$(JNI_INCLUDE_DIR)/macosx-x64/include/darwin
        CC                  := x86_64-apple-darwin17-cc
        CXX                 := x86_64-apple-darwin17-c++
    endif
    ifneq (,$(filter $(ARCH), x86 x32))
        TARGET_ARCH         := x86
        JNI_INCLUDE			:= -I$(JNI_INCLUDE_DIR)/macosx-x64/include -I$(JNI_INCLUDE_DIR)/macosx-x64/include/darwin
        CC                  := i386-apple-darwin17-cc
        CXX                 := i386-apple-darwin17-c++
    endif
    ifneq (,$(filter $(ARCH), arm64 aarch64))
        TARGET_ARCH         := aarch64
        JNI_INCLUDE			:= -I$(JNI_INCLUDE_DIR)/macosx-aarch64/include -I$(JNI_INCLUDE_DIR)/macosx-aarch64/include/darwin
        CC                  := o64-clang -c -fPIC -arch arm64
        CXX                 := o64-clang++ -c -fPIC -arch arm64
    endif
endif
#  IOS
ifneq (,$(filter $(PLATFORM), ios IOS))
    LAB_NAME				:= lib$(LAB_NAME)
    TARGET_PLATFORM         := ios
    EXT                     := .dylib
endif
ifneq (,$(filter $(PLATFORM), Android android))
    LAB_NAME				:= lib$(LAB_NAME)
    TARGET_PLATFORM         := android
    EXT                     := .so
endif


ifneq (,$(filter $(TARGET_PLATFORM), unknown))
    $(error $(RED)Unsupported platform $(TARGET_PLATFORM)$(RESET))
endif
ifneq (,$(filter $(TARGET_ARCH), unknown))
    $(error $(RED)Unsupported arch "$(ARCH)" platform $(TARGET_PLATFORM)$(RESET))
endif

LAB_NAME     := $(call get-lib-name)
BUILD_SCRIPT := $(CXX) $(SHARED_FLAG) $(CC_ARGS) -fPIC $(SRC_FILES) $(JNI_INCLUDE) -I$(SRC_INCLUDES) -o $(OUT_DIR)/$(LAB_NAME)$(EXT)


all:
	mkdir -p $(OUT_DIR)
	@echo $(LAB_NAME) $(SRC_INCLUDES)  $(SRC_FILES)
	@echo $(JNI_INCLUDE)
	@echo $(BUILD_SCRIPT)
	@$(BUILD_SCRIPT)

clean:
	rm -rf $(OUT_DIR)

.PHONY: all clean